package com.dap.cms.governance.service.impl;

import com.dap.cms.base.impl.BaseServiceImpl;
import com.dap.cms.governance.common.RequestHolder;
import com.dap.cms.governance.po.CmsApplicationFormPo;
import com.dap.cms.governance.po.CmsCheckInfoPo;
import com.dap.cms.governance.po.CmsManagerPo;
import com.dap.cms.governance.po.CmsUserPo;
import com.dap.cms.governance.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @description: t_cms_check_info表对应的服务接口实现类
 * @author: tigerlin
 * @createTime:2018/09/19
 * @update:修改内容
 * @author: tigerlin
 * @updateTime:2018/09/19
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CmsCheckInfoServiceImpl extends BaseServiceImpl<CmsCheckInfoPo>
        implements CmsCheckInfoService {
    @Autowired
    private CmsUserService cmsUserService;
    @Autowired
    private CmsManagerService cmsManagerService;
    @Autowired
    private CmsMessageService cmsMessageService;
    @Autowired
    private CmsApplicationFormService cmsApplicationFormService;
    @Autowired
    private CmsComponentManagerService cmsComponentManagerService;

    @Override
    public void add(CmsApplicationFormPo cmsApplicationFormPo) {
        CmsCheckInfoPo cmsCheckInfoPo = new CmsCheckInfoPo();
        cmsCheckInfoPo.setApplicationId(cmsApplicationFormPo.getId());
        //初始化时同一设置为0：申请未处理
        cmsCheckInfoPo.setHandingResult(0);
        CmsUserPo cmsUserPo = new CmsUserPo();
        cmsUserPo.setSysId(cmsApplicationFormPo.getSysId());
        cmsUserPo.setUserFlag(2);
        cmsUserPo = cmsUserService.get(cmsUserPo);
        CmsManagerPo cmsManagerPo = new CmsManagerPo();
        cmsManagerPo.setUsername(cmsUserPo.getName());
        cmsManagerPo = cmsManagerService.get(cmsManagerPo);
        cmsCheckInfoPo.setToUserId(cmsManagerPo.getId());
        cmsCheckInfoPo.setToUserType(2);
        cmsMessageService.addMsg(
                cmsManagerPo.getId(),
                1,
                "有新的申请单待审核！",
                "您好，尊敬的用户：\n" + cmsManagerPo.getUsername() + ",\n" +
                        RequestHolder.getCurrentUser().getUsername() + "向您提交了:\n" + cmsApplicationFormPo.getComponentName() + "接入申请" +
                        ",请您审批！",
                null
        );
        //添加申请详情
        save(cmsCheckInfoPo);
    }

    @Override
    public void examine(CmsCheckInfoPo cmsCheckInfoPo,String otherMsg) {
        update(cmsCheckInfoPo);
        CmsApplicationFormPo cmsApplicationFormPo = new CmsApplicationFormPo();
        cmsApplicationFormPo.setId(cmsCheckInfoPo.getApplicationId());
        cmsApplicationFormPo = cmsApplicationFormService.get(cmsApplicationFormPo);
        //case:审核驳回
        if (cmsCheckInfoPo.getHandingResult() == 2) {
            cmsApplicationFormPo.setStatus(2);
            cmsApplicationFormService.update(cmsApplicationFormPo);
            cmsMessageService.addMsg(
                    cmsApplicationFormPo.getRequestUserId(),
                    2,
                    "组件申请驳回",
                    "您的组件" + cmsApplicationFormPo.getComponentName() + "接入申请未通过审核",
                    otherMsg
            );
            return;
        }
        //case:审核通过
        if (cmsCheckInfoPo.getHandingResult() == 1) {
            if (cmsCheckInfoPo.getToUserType() == 4) {
                cmsApplicationFormPo.setStatus(1);
                cmsApplicationFormService.update(cmsApplicationFormPo);
                cmsMessageService.addMsg(
                        cmsApplicationFormPo.getRequestUserId(),
                        2,
                        "组件申请通过",
                        "恭喜，您的组件" + cmsApplicationFormPo.getComponentName() + "接入申请通过审核，可以参照接入手册接入组件",
                        otherMsg
                );
                return;
            }
            //组件管理员向超级管理员提交申请
            if (cmsCheckInfoPo.getToUserType() == 3) {
                CmsCheckInfoPo checkInfoPo = new CmsCheckInfoPo();
                checkInfoPo.setApplicationId(cmsCheckInfoPo.getApplicationId());
                checkInfoPo.setHandingResult(0);
                checkInfoPo.setToUserType(4);
                CmsManagerPo cmsManagerPo = new CmsManagerPo();
                cmsManagerPo.setManagerFlag(4);
                cmsManagerPo = cmsManagerService.get(cmsManagerPo);
                checkInfoPo.setToUserId(cmsManagerPo.getId());
                save(checkInfoPo);
                cmsMessageService.addMsg(
                        cmsManagerPo.getId(),
                        1,
                        "有新的申请单待审核！",
                        "您好，尊敬的管理员：\n" + cmsManagerPo.getUsername() + ",\n" +
                                RequestHolder.getCurrentUser().getUsername() + "向您提交了:\n" + cmsApplicationFormPo.getComponentName() + "接入申请" +
                                ",请您审批！",
                        otherMsg
                );
                return;
            }
            //系统负责人向组件管理员提交申请
            if (cmsCheckInfoPo.getToUserType() == 2) {
                CmsCheckInfoPo checkInfoPo = new CmsCheckInfoPo();
                checkInfoPo.setApplicationId(cmsCheckInfoPo.getApplicationId());
                checkInfoPo.setHandingResult(0);
                checkInfoPo.setToUserType(3);
                CmsManagerPo managerPo=cmsManagerService.selectManagerByComponentId(cmsApplicationFormPo.getComponentId());
                checkInfoPo.setToUserId(managerPo.getId());
                save(checkInfoPo);
                cmsMessageService.addMsg(
                        managerPo.getId(),
                        1,
                        "有新的申请单待审核！",
                        "您好，尊敬的组件管理员," +
                                RequestHolder.getCurrentUser().getUsername() + "向您提交了:\n" + cmsApplicationFormPo.getComponentName() + "接入申请" +
                                ",请您审批！",
                        otherMsg
                );
            }
        }

    }
}

